package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.Log;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.DateFormatUtil;
import com.itheima.util.JwtUtil;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.VideoVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;

@Service
public class UserManager {

    @Reference
    private UserInfoService userInfoService;

    @Reference
    private VideoService videoService;

    @Reference
    private MovementService movementService;

    @Reference
    private CommentService commentService;

    //查询用户列表
    public PageBeanVo findUserList(Integer pageNum, Integer pageSize) {
        return userInfoService.findUserList(pageNum, pageSize);
    }


    //查询用户详情
    public UserInfo findUserInfo(Long userId) {

        return userInfoService.findById(userId);

    }


    //用户视频
    public PageBeanVo findUserVideoList(Long uid, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = videoService.findUserVideoList(uid, pageNum, pageSize);

        //获取items
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();

        //遍历封装
        List<VideoVo> videoVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                UserInfo userInfo = userInfoService.findById(video.getUserId());

                //封装vo
                VideoVo videoVo = BeanUtil.copyProperties(userInfo, VideoVo.class);

                BeanUtil.copyProperties(video,videoVo);

                //手动封装未赋值字段
                videoVo.setCover(video.getPicUrl());   //封面
                videoVo.setSignature(video.getText());  //内容

                //存入集合
                videoVoList.add(videoVo);
            }
        }

        pageBeanVo.setItems(videoVoList);

        return pageBeanVo;
    }



    //查询动态
    public PageBeanVo findMovementList(Long uid, Integer stateInt, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = movementService.findMovementByUid(uid, stateInt, pageNum, pageSize);

        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();

        //判断封装
        List<MovementVo> movementVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                Long userId = movement.getUserId();

                //获取用户信息
                UserInfo userInfo = userInfoService.findById(userId);

                //封装信息
                MovementVo movementVo = new MovementVo();
                movementVo.setUserInfo(userInfo);
                movementVo.setMovement(movement);
                movementVo.setCreateDate(DateUtil.offsetDay(new Date(movement.getCreated()), 0).toString());

                //添加进集合
                movementVoList.add(movementVo);
            }
        }

        pageBeanVo.setItems(movementVoList);

        return pageBeanVo;
    }



    //查询动态详情
    public MovementVo findMovementById(String movementId) {
        //1. 根据动态id,调用service查询
        Movement movement = movementService.findMovementById(movementId);

        //2. 处理返回vo
        UserInfo userInfo = userInfoService.findById(movement.getUserId());
        MovementVo movementVo = new MovementVo();
        movementVo.setUserInfo(userInfo);
        movementVo.setMovement(movement);
        movementVo.setCreateDate(DateUtil.offsetDay(new Date(movement.getCreated()), 0).toString());

        return movementVo;
    }


    //动态评论列表
    public PageBeanVo findMovementComment(String messageID, Integer pageNum, Integer pageSize) {

        PageBeanVo pageBeanVo = commentService.findMovementComment(messageID, pageNum, pageSize);

        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        List<CommentVo> commentVoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                //创建vo
                CommentVo commentVo = new CommentVo();

                //用户信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                commentVo.setAvatar(userInfo.getAvatar());
                commentVo.setNickname(userInfo.getNickname());

                //评论信息
                commentVo.setId(comment.getId().toHexString());
                commentVo.setContent(comment.getContent());
                commentVo.setCreateDate(DateFormatUtil.format(new Date(comment.getCreated())));

                commentVoList.add(commentVo);
            }
        }

        pageBeanVo.setItems(commentVoList);
        return pageBeanVo;
    }

    //消息通过
    public void messagesPass(String[] str) {
        movementService.messagesPass(str);
    }

    //消息拒绝
    public void messagesReject(String[] str) {
        movementService.messagesReject(str);
    }





}
